package com.example.smartwaterapi.controller;

import com.example.smartwaterapi.common.api.RestApiResponse;
import com.example.smartwaterapi.entity.DailyStats;
import com.example.smartwaterapi.service.DailyStatsService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * 每日统计控制器
 */
@Slf4j
@Tag(name = "每日统计管理API", description = "每日饮水统计相关接口")
@RestController
@RequestMapping("/api/daily-stats")
public class DailyStatsController {

    @Autowired
    private DailyStatsService dailyStatsService;

    @Operation(summary = "获取每日统计详情", description = "根据ID获取每日统计信息")
    @GetMapping("/{id}")
    public RestApiResponse<DailyStats> getDailyStats(@Parameter(description = "统计ID") @PathVariable Long id) {
        log.info("获取每日统计详情, id: {}", id);
        try {
            DailyStats stats = dailyStatsService.getById(id);
            if (stats == null) {
                log.warn("未找到每日统计记录, id: {}", id);
                return RestApiResponse.notFound("统计记录不存在");
            }
            log.info("成功获取每日统计记录, id: {}", id);
            return RestApiResponse.ok(stats);
        } catch (Exception e) {
            log.error("获取每日统计详情异常, id: {}", id, e);
            return RestApiResponse.error("获取统计记录失败: " + e.getMessage());
        }
    }
    
    @Operation(summary = "创建每日统计", description = "创建新的每日统计记录")
    @PostMapping
    public RestApiResponse<DailyStats> createDailyStats(@RequestBody DailyStats dailyStats) {
        log.info("创建每日统计记录: {}", dailyStats);
        try {
            boolean success = dailyStatsService.save(dailyStats);
            if (success) {
                log.info("每日统计记录创建成功, id: {}", dailyStats.getId());
                return RestApiResponse.created(dailyStats);
            } else {
                log.warn("每日统计记录创建失败");
                return RestApiResponse.error("创建统计记录失败");
            }
        } catch (Exception e) {
            log.error("创建每日统计记录异常", e);
            return RestApiResponse.error("创建统计记录异常: " + e.getMessage());
        }
    }
    
    @Operation(summary = "更新每日统计", description = "更新已有的每日统计记录")
    @PutMapping("/{id}")
    public RestApiResponse<Boolean> updateDailyStats(
            @Parameter(description = "统计ID") @PathVariable Long id,
            @RequestBody DailyStats dailyStats) {
        log.info("更新每日统计记录, id: {}, data: {}", id, dailyStats);
        try {
            dailyStats.setId(id);
            boolean success = dailyStatsService.updateById(dailyStats);
            if (success) {
                log.info("每日统计记录更新成功, id: {}", id);
                return RestApiResponse.ok(true);
            } else {
                log.warn("每日统计记录更新失败, id: {}", id);
                return RestApiResponse.notFound("更新统计记录失败，记录可能不存在");
            }
        } catch (Exception e) {
            log.error("更新每日统计记录异常, id: {}", id, e);
            return RestApiResponse.error("更新统计记录异常: " + e.getMessage());
        }
    }
    
    @Operation(summary = "删除每日统计", description = "删除指定的每日统计记录")
    @DeleteMapping("/{id}")
    public RestApiResponse<Boolean> deleteDailyStats(@Parameter(description = "统计ID") @PathVariable Long id) {
        log.info("删除每日统计记录, id: {}", id);
        try {
            boolean success = dailyStatsService.removeById(id);
            if (success) {
                log.info("每日统计记录删除成功, id: {}", id);
                return RestApiResponse.ok(true);
            } else {
                log.warn("每日统计记录删除失败, id: {}", id);
                return RestApiResponse.notFound("删除统计记录失败，记录可能不存在");
            }
        } catch (Exception e) {
            log.error("删除每日统计记录异常, id: {}", id, e);
            return RestApiResponse.error("删除统计记录异常: " + e.getMessage());
        }
    }
} 